package com.biz.primus.ms.geo.api;

import com.biz.primus.base.vo.PageVo;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.geo.enums.GeoLevel;
import com.biz.primus.model.geo.vo.*;
import com.biz.primus.ms.geo.service.GeoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * GeoApiController
 *
 * @author jia JKLiues
 * @date 2017年09月19日
 * @reviewer
 * @see
 */
@RestController
@RequestMapping("soa/geoService")
public class GeoApiController {

    private final GeoService geoService;

    @Autowired
    public GeoApiController(GeoService geoService) {
        this.geoService = geoService;
    }

    @ApiOperation(value = "通过省查城市", notes = "从缓存中查")
    @GetMapping("findCitiesByProvinceId")
    public List<AreaResponseVo> findCitiesByProvinceId(
            @ApiParam(value = "省ID") @RequestParam("provinceId") Long provinceId) {
        return geoService.findCitiesByProvinceId(provinceId.intValue());
    }

    @ApiOperation(value = "通过城市查区域", notes = "从缓存中查")
    @GetMapping("findDistrictsByCityId")
    public List<AreaResponseVo> findDistrictsByCityId(
            @ApiParam(value = "城市ID") @RequestParam("cityId") Long cityId) {
        return geoService.findDistrictsByCityId(cityId.intValue());
    }

    @ApiOperation("从redis查省")
    @GetMapping("findProvince")
    public ProvinceResponseVo findProvince(
            @ApiParam(value = "省ID") @RequestParam("provinceId") Long provinceId) {
        return geoService.findProvince(provinceId.intValue());
    }

    @ApiOperation("从redis查城市")
    @GetMapping("findCity")
    public CityResponseVo findCity(
            @ApiParam(value = "城市ID") @RequestParam("cityId") Long cityId) {
        return geoService.findCity(cityId.intValue());
    }


    @ApiOperation("从redis查区域")
    @GetMapping("findDistrict")
    public DistrictResponseVo findDistrict(
            @ApiParam(value = "区域ID") @RequestParam("districtId") Long districtId) {
        return geoService.findDistrict(districtId.intValue());
    }


    @ApiOperation("定位")
    @PostMapping("decodeLocation")
    public CityItemRespVo decodeLocation(@RequestBody LocationDecodeRequestVo reqVo) {
        return geoService.decodeLocation(reqVo);
    }

    @ApiOperation("定位")
    @GetMapping("decodeLocationByCityAndDistrict")
    public LocationAreaVo decodeLocation(
            @ApiParam(value = "城市名") @RequestParam("cityName") String cityName,
            @ApiParam(value = "区域名") @RequestParam("districtName") String districtName) {
        return geoService.decodeLocation(cityName, districtName);
    }


    @ApiOperation(value = "百度名称查省", notes = "从redis查")
    @GetMapping("findProvinceByBaiduName")
    public Optional<ProvinceResponseVo> findProvinceByBaiduName(
            @ApiParam(name = "省名称") @RequestParam("baiduName") String baiduName) {
        return geoService.findProvinceByBaiduName(baiduName);
    }

    @ApiOperation(value = "百度名称查城市", notes = "从redis查")
    @GetMapping("findCityByBadiuName")
    public Optional<CityResponseVo> findCityByBadiuName(
            @ApiParam(name = "城市名称") @RequestParam("baiduName") String baiduName) {
        return geoService.findCityByBadiuName(baiduName);
    }

    @ApiOperation(value = "百度名称查区域", notes = "从redis查")
    @GetMapping("findDistrictByBaiduName")
    public Optional<DistrictResponseVo> findDistrictByBaiduName(@RequestParam("baiduName") String baiduName) {
        return geoService.findDistrictByBaiduName(baiduName);
    }

    @ApiOperation("同步Redis")
    @PostMapping("syn2Redis")
    public void syncMysql2Redis() {
        geoService.syncMysql2Redis();
    }

    @ApiOperation("校验区域是否有效")
    @PostMapping("checkedAreaEffective")
    public boolean checkedAreaEffective(
            @ApiParam(name = "经度") @RequestParam("lon") BigDecimal lon,
            @ApiParam(name = "纬度") @RequestParam("lat") BigDecimal lat,
            @ApiParam(name = "省ID", allowMultiple = true) @RequestParam("provinceIds") Long... provinceIds) {
        return geoService.checkedAreaEffective(lon, lat, provinceIds);
    }

    @ApiOperation(value = "模糊查询城市", notes = "mysql中查")
    @GetMapping
    public List<CityItemVo> listByNameOfLike(
            @ApiParam(name = "城市名称") @RequestParam("cityName") String cityName) {
        return geoService.listByNameOfLike(cityName);
    }

    @ApiOperation("查全省信息")
    @GetMapping("getProvinces")
    public List<ProvincesItemResponseVo> getProvinces() {
        return geoService.getProvinces();
    }

    @ApiOperation("获取全部的省及城市")
    @GetMapping("getProvinceCities")
    public List<ProvinceCitiesItemResponseVo> getProvinceCities() {
        return geoService.getProvinceCities();
    }

    @ApiOperation("获取全部的城市及所在省")
    @GetMapping("findAllCitiesWithProvinceId")
    public List<AreaResponseVo> findAllCitiesWithProvinceId() {
        return geoService.findAllCitiesWithProvinceId();
    }

    @ApiOperation(value = "查全省信息", notes = "从缓存中查")
    @GetMapping("findAllProvince")
    public List<AreaResponseVo> findAllProvince() {
        return geoService.findAllProvince();
    }

    @ApiOperation("全省")
    @GetMapping("buildPushProvince")
    public List<SimpleProvinceVo> buildPushProvince() {
        return geoService.buildPushProvince();
    }

    @ApiOperation("全市")
    @GetMapping("buildPushCity")
    public List<SimpleCityVo> buildPushCity() {
        return geoService.buildPushCity();
    }

    @ApiOperation("全区")
    @GetMapping("buildPushDistrict")
    public List<SimpleDistrictVo> buildPushDistrict() {
        return geoService.buildPushDistrict();
    }

    @ApiOperation("记录推送日志")
    @PostMapping("doPushLog")
    public void doPushLog(@ApiParam("推送数量") @RequestParam("size") Integer size,
                          @ApiParam("数据等级") @RequestParam("level") GeoLevel level,
                          @ApiParam("返回code") @RequestParam("returnCode") Integer returnCode) {
        geoService.doPushLog(size, level, returnCode);
    }

    @ApiOperation("查全部的市")
    @GetMapping("findAllCities")
    public List<AreaResponseVo> findAllCities() {
        return geoService.findAllCities();
    }

    @ApiOperation("查全部区")
    @GetMapping("findAllDistricts")
    public List<AreaResponseVo> findAllDistricts() {
        return geoService.findAllDistricts();
    }

    /**
     * ---------------------分隔线------后台功能--------------------------
     */
    @ApiOperation("更新或修改省信息")
    @PostMapping("updateProvinceInfo")
    public void updateProvinceInfo(@RequestBody GeoUpdateReqVo reqVo) {
        geoService.updateProvince(reqVo);
    }

    @ApiOperation("更新或修改市信息")
    @PostMapping("updateCityInfo")
    public void updateCityInfo(@RequestBody GeoUpdateReqVo reqVo) {
        geoService.updateCity(reqVo);
    }

    @ApiOperation("更新或修改区信息")
    @PostMapping("updateDistrictInfo")
    public void updateDistrictInfo(@RequestBody GeoUpdateReqVo reqVo) {
        geoService.updateDistrict(reqVo);
    }

    @ApiOperation("显示数据库中全部的省信息")
    @PostMapping("provinces")
    public SpringPageVO<ProvinceListRespVo> backProvinceList(@RequestBody PageVo reqVo) {
        return geoService.listProvince(reqVo);
    }

    @ApiOperation("显示数据库中市信息")
    @PostMapping("cities")
    public SpringPageVO<CityListRespVo> backCitesList(@RequestBody CityPageListReqVo reqVo) {
        return geoService.listCity(reqVo.getProvinceId(), reqVo);
    }

    @ApiOperation("显示数据库中区信息")
    @PostMapping("districts")
    public SpringPageVO<DistrictListRespVo> backDistrictList(@RequestBody DistrictPageListReqVo reqVo) {
        return geoService.listDistrict(reqVo.getCityId(), reqVo);
    }

    @ApiOperation(value = "后台获取省详情")
    @GetMapping("findProvinceById")
    public ProvinceDetailRespVo findProvinceById(@ApiParam(value = "省ID") @RequestParam(value = "provinceId", required = false) Long provinceId) {
        return geoService.findProvinceById(provinceId);
    }

    @ApiOperation(value = "后台获取市详情")
    @GetMapping("findCityById")
    public CityDetailRespVo findCityById(@ApiParam(value = "市ID") @RequestParam(value = "cityId", required = false) Long cityId) {
        return geoService.findCityById(cityId);
    }

    @ApiOperation(value = "后台获取省详情")
    @GetMapping("findDistrictById")
    public DistrictDetailRespVo findDistrictById(@ApiParam(value = "区ID") @RequestParam(value = "districtId", required = false) Long districtId) {
        return geoService.findDistrictById(districtId);
    }
}